package cn.e3mall.content.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.e3mall.common.jedis.JedisClient;
import cn.e3mall.common.pojo.EasyUIDataGridResult;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.common.utils.JsonUtils;
import cn.e3mall.content.service.ContentService;
import cn.e3mall.mapper.TbContentMapper;
import cn.e3mall.pojo.TbContent;
import cn.e3mall.pojo.TbContentExample;
import cn.e3mall.pojo.TbItem;
import cn.e3mall.pojo.TbContentExample.Criteria;

@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${CONTENT_LIST}")
	private String CONTENT_LIST;
	
	/**
	 * 	查询内容列表
	 */
	@Override
	public EasyUIDataGridResult getContentListById(long id,int page,int rows) {
		//设置分页信息
		PageHelper.startPage(page, rows);
		
		//设置查询条件
		TbContentExample example = new TbContentExample();
		
		Criteria criteria = example.createCriteria();
		
		criteria.andCategoryIdEqualTo(id);
		
		List<TbContent> contentList = contentMapper.selectByExampleWithBLOBs(example);
		
		//提取contentList的分页信息
		PageInfo<TbContent> contentPageInfo = new PageInfo<>(contentList);
		
		//设置EasyUIDataGridResult
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		
			//设置内容列表
		result.setRows(contentList);
			//设置总记录数
		result.setTotal(contentPageInfo.getTotal());
		
		return result;
	}
	
	/**
	 * 	添加内容
	 */
	
	@Override
	public E3Result addContent(TbContent content) {
		//将内容数据插入到内容表
		content.setCreated(new Date());
		content.setUpdated(new Date());
		//插入到数据库
		contentMapper.insert(content);
		
		//删除缓存中对应内容分类下的所有内容，这种属于粗粒度删除
		jedisClient.hdel(CONTENT_LIST,content.getCategoryId().toString());
		
		return E3Result.ok();
	}

	/**
	 * 	编辑内容
	 */
	@Override
	public E3Result editContent(TbContent content) {
		
		//将内容数据插入到内容表
		content.setCreated(null);
		content.setUpdated(new Date());
		
		//插入到数据库
		contentMapper.updateByPrimaryKeySelective(content);
		
		//删除缓存中对应内容分类，这种属于粗粒度删除
		jedisClient.hdel(CONTENT_LIST,content.getCategoryId().toString());
		
		return E3Result.ok();		
	}

	/**
	 * 	删除内容列表
	 */
	@Override
	public E3Result deleteContent(Long[] ids) {
		//查询内容id对应的内容对象
		TbContent content = contentMapper.selectByPrimaryKey(ids[0]);
		
		//遍历删除
		for(Long id : ids) {
			//根据id删除内容
			contentMapper.deleteByPrimaryKey(id);
		}
		
		//删除缓存中对应内容分类，这种属于粗粒度删除
		jedisClient.hdel(CONTENT_LIST,content.getCategoryId().toString());
		
		return E3Result.ok();
	}
	
	/**
	 * 	根据内容分类id查询内容列表
	 * <p>Title: getContentListByCid</p>
	 * <p>Description: </p>
	 * @param cid
	 * @return
	 * @see cn.e3mall.content.service.ContentService#getContentListByCid(long)
	 */
	@Override
	public List<TbContent> getContentListByCid(long cid) {
		//查询缓存
		try {
			String content_list = jedisClient.hget(CONTENT_LIST, cid+"");
			
			//若缓存有数据
			if(StringUtils.isNoneBlank(content_list)) {
				return JsonUtils.jsonToList(content_list, TbContent.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		//设置查询条件
		criteria.andCategoryIdEqualTo(cid);
		//执行查询
		List<TbContent> list = contentMapper.selectByExampleWithBLOBs(example);
		
		//添加缓存
		try {
			jedisClient.hset(CONTENT_LIST, cid+"",JsonUtils.objectToJson(list));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
		
	}
	
}
